﻿// Generic container movieclip class with a couple of methods for handling sub-movies

class com.spikything.utils.ContainerMC extends MovieClip {

	private	var pingpongCount 	:Number;

	function ContainerMC () {}

	// PUBLIC METHODS

	public function attach (theMovie:String, initObject:Object, customDepth:Number):MovieClip {
		if (customDepth==undefined) var customDepth = this.getNextHighestDepth();
		return attachMovie(theMovie, "m"+customDepth, customDepth, initObject);
	}

	public function withChildren (method:String, args:Object):Void {
		for (var i in this) this[i][method](args);
	}

	public function removeChildren ():Void {
		withChildren("removeMovieClip");
	}

	public function pingpong (times:Number):Void {
		pingpongCount = times ? times : 0;
		this.onEnterFrame = stepForward;
		stepForward();
	}

	public function stepForward ():Void {
		nextFrame();
		if (_currentframe==_totalframes) this.onEnterFrame = stepBack;
	}

	public function stepBack ():Void {
		prevFrame();
		if (_currentframe==1) {
			if (pingpongCount) {
				pingpongCount--;
				this.onEnterFrame = stepForward;
			} else {
				delete this.onEnterFrame;
			}
		}
	}

	public function moveTo (x:Number,y:Number):Void {
		_x = x;
		_y = y;
	}

	public function scaleTo (scale:Number):Void {
		_xscale = _yscale = scale;
	}

	public function moveToTop ():Void {
		swapDepths(_parent.getNextHighestDepth());
	}

}